package controllers;

import java.awt.event.ActionEvent;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import model.ComplaintModel;
import model.ComplaintReportModel;
import model.SubscriptionRequestModel;
import model.UserPermissions;
import model.DateRequestModel;
import model.dateModel;
import view.CompareReportGUI;
import view.ComplaintReportGUI;
import view.ViewMonthlyGUI;
import client.CliMessage;

public class ViewMonthlyController extends AbstractController implements IValidateInput
{	
	private ViewMonthlyGUI  monthlyView;
	private DateRequestModel daModel;
	
	public ViewMonthlyController(  ViewMonthlyGUI  monthlyView, DateRequestModel daModel) 
	{
		super(monthlyView, daModel);
		this.daModel = daModel;
		this.monthlyView = monthlyView;
             
            
        String searchQuery = "search"	+"SELECT * "
                   	+" FROM complaint_report" ;
                 
        sendToServer(searchQuery);
        
		 	   searchQuery = "search"	+"SELECT * "
            	+" FROM archive_report" ;
		
        sendToServer(searchQuery);
                
               searchQuery = "search"	+"SELECT * "
                   	+" FROM package_report" ;
                     
        sendToServer(searchQuery);
                    
	}
	
	@Override
	public boolean validateInput() 
	{
		int index=monthlyView.getItemSelectedIndex();
		
		if(index<1)
		{
			
			monthlyView.displayWrnMessage("You MUST Choose Package Before Purchase ");
			
			return false;
		}
		
		
	return true;	
	}
	
	/**
	 * This is setButton method-this method add listeners to buttons in Buy a Subscription window.
	 * 
	 */
	
	private void setButton()
	{
		
		monthlyView.addCompareActionListener(new CompareListener(this, monthlyView));
	}
				
	

	class CompareListener extends AbstractListener
	{
		private ViewMonthlyGUI  monthlyView;
		private ViewMonthlyController previusObserver;
		private UserPermissions up;
		
		public CompareListener(ViewMonthlyController previusObserver,  ViewMonthlyGUI  monthlyView)
		{
			this.monthlyView=monthlyView;
			this.previusObserver=previusObserver;
		}
		
		@Override
		public void actionPerformed(ActionEvent arg0) 
		{
			if(!validateInput())
				return;   
			
			String DATE1 = monthlyView.getDATE1();
			String searchQuery = null;
			CompareReportGUI compView = new CompareReportGUI();
			ComplaintModel mod = new ComplaintModel();	
			CompareReportController.count = 0;
			CompareReportController buySubDetCon = new CompareReportController(compView, mod);
			
					
			
			
			//add observer to new controller and delete the rest
			setCurrentObserver(buySubDetCon);						
			
			//add a listener to "back" button in buy sub  window
			setBackListener(previusObserver, compView);
			
			System.out.println ("INDEX: " + monthlyView.getIndex() + " DATE: " + DATE1 );
			
			if ( monthlyView.getIndex() == 1 )
			{
				searchQuery = "search"	+ "SELECT *"
												+ " FROM complaint_report AS c"
												+ " WHERE c.date = '" + DATE1 + "'";
			}
			
			if ( monthlyView.getIndex() == 2 )
			{
				searchQuery = "search"	+ "SELECT *"
												+ " FROM archive_report AS a"
												+ " WHERE a.date = '" + DATE1 + "'";
			}
			
			if ( monthlyView.getIndex() == 3 )
			{
				searchQuery = "search"	+ "SELECT *"
                        						+ " FROM package_report AS p"
                        						+ " WHERE p.date = '" + DATE1 + "'";
			}
			
			sendToServer(searchQuery);	
			
			if (monthlyView.getComp() == true)
			{
				String DATE2 = monthlyView.getDATE2();
				
				if ( monthlyView.getIndex() == 1 )
				{
					searchQuery = "search"	+ "SELECT *"
													+ " FROM complaint_report AS c"
													+ " WHERE c.date = '" + DATE2 + "'";
				}
				
				if ( monthlyView.getIndex() == 2 )
				{
					searchQuery = "search"	+ "SELECT *"
													+ " FROM archive_report AS a"
													+ " WHERE a.date = '" + DATE2 + "'";
				}
				
				if ( monthlyView.getIndex() == 3 )
				{
					searchQuery = "search"	+ "SELECT *"
	                        						+ " FROM package_report AS p"
	                        						+ " WHERE p.date = '" + DATE2 + "'";
				}
				
				sendToServer(searchQuery);
				
			}
			
			
		}
	}



	@Override
	public void handleDBRequestSuccess()
	{
		monthlyView.displayWrnMessage("Dates Found:");
		
	}

	@Override
	public void handleDBSearchResult(CliMessage dbResult) 
	{	
		int i;
		ArrayList<dateModel>  tReq;
		
		if 	(dbResult!=null)	
		{
			ArrayList<HashMap<String, String>> srs = dbResult.getSrs();
			tReq= daModel.getDateRequest();
			
	    	int rows=srs.size();
		    for(i = 0 ; i < rows; i++)
		    {
		    	Map<String, String> dateList  = srs.get(i);				
		    	if(dateList.get("DATE")!=null)
		    	{
			    	System.out.println(dateList.get("DATE"));
		    		tReq.add(new dateModel (dateList.get("DATE")));
			    	System.out.println(tReq.get(i));
		    	}			
		    }	
		    
		    Map<String, String> dateList  = srs.get(0);		    
			if ( dateList.get("CID") != null )
			{
				System.out.println ("COMPLAINT DATABASE size: " + tReq.size());
				monthlyView.setCompArrayDATE(tReq);	
					tReq.clear();
			}
			
			if ( dateList.get("AID") != null )
			{
				System.out.println ("ARCHIVE DATABASE size: " + tReq.size());
				monthlyView.setArchArrayDATE(tReq);
					tReq.clear();
			}
			
			if ( dateList.get("PID") != null )
			{
				System.out.println ("PACKAGE DATABASE size: " + tReq.size());
				monthlyView.sePackArrayDATE(tReq);
					tReq.clear();
				setButton();
			}
		}
		
		  //display main window
		  monthlyView.setCombo();
		  monthlyView.setVisible(true);

	}

	@Override
	public void handleNullSearchResult() 
	{
	    handleDBSearchResult(null) ;	
	}

}